package com.sun.redis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Spring整合Redis实现
 */
@RestController
@RequestMapping("/redis")
public class RedisTemplateController {

    @Autowired
    private RedisTemplate redisTemplate;

    // http://localhost:8080/redis/set/test
    @GetMapping(value = "/set/{key}")
    public String set(@PathVariable String key) {
        redisTemplate.opsForValue().set("key-" + key, "value-" + key);
        return "set success";
    }

    // http://localhost:8080/redis/get/test
    @GetMapping(value = "/get/{key}")
    public String get(@PathVariable String key) {
        return (String) redisTemplate.opsForValue().get("key-" + key);
    }

    // http://localhost:8080/redis/del/test
    @GetMapping(value = "/del/{key}")
    public String del(@PathVariable String key) {
        redisTemplate.delete("key-" + key);
        return "delete success";
    }

}
